
#include "CEntity.h"

std::vector<CEntity*> CEntity::entityList;
//std::vector<CEntityCol> CEntity::collisionList;
std::priority_queue<entityEffect> CEntity::entityEffects;

CEntity::CEntity(){
	entitySurface = NULL;
	x = y = 0.0f;
	speedX = speedY = 0.0f;
	accelX = accelY = 0.0f;
	maxSpeed = 10.0f;
	effectFlags = 0;
}

void CEntity::setPos(float x, float y){
	this->x = x;
	this->y = y;
}

float CEntity::getPosX(){
	return x;
}

float CEntity::getPosY(){
	return y;
}

float CEntity::getSpeedX(){
	return speedX;
}

float CEntity::getSpeedY(){
	return speedY;
}

void CEntity::onLoop(){

	speedX += accelX * CFPS::fpsControl.getSpeedFactor();
	speedY += accelY * CFPS::fpsControl.getSpeedFactor();

	if(speedX > maxSpeed)
			speedX = maxSpeed;

	if(speedX < -maxSpeed)
			speedX = -maxSpeed;

	if(speedY > maxSpeed)
			speedY = maxSpeed;

	if(speedY < -maxSpeed)
			speedY = -maxSpeed;

	onMove();
}

void CEntity::onMove(){
	
	if(speedX == 0 || speedY == 0)
		return;

	float newX, newY;

	newX = x + (speedX * CFPS::fpsControl.getSpeedFactor());
	newY = y + (speedX * CFPS::fpsControl.getSpeedFactor());

	setPos(newX,newY);
}

void CEntity::onRender(SDL_Surface *destSurface){
	
	if(entitySurface == NULL || destSurface == NULL)
		return;

	CSurface::OnDraw(destSurface,entitySurface, (int) x, (int) y);
}

bool CEntity::onLoad(char* file){

	if( (entitySurface = CSurface::OnLoad(file)) == NULL )
		return false;

	return true;
}

void CEntity::onCleanup(){

	if(entitySurface)
		SDL_FreeSurface(entitySurface);

	entitySurface = NULL;
}

void CEntity::addEffectFlag(int flag){
	effectFlags = effectFlags | flag;
}

void CEntity::removeEffectFlag(int flag){
	if( effectFlags & flag )
		effectFlags = effectFlags ^ flag;
}

void CEntity::clearEffectFlags(){
	effectFlags = 0;
}

bool operator < (const entityEffect& a, const entityEffect& b){
	return (a.offTime > b.offTime);
}